/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */
package com.hopes.bizz.poll.town.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hopes.bizz.poll.town.dto.PollTownDataAlterDto;
import com.hopes.bizz.poll.town.dto.TownHisDataDTO;
import com.hopes.bizz.poll.town.dto.TownHisTrendDTO;
import com.hopes.bizz.poll.town.entity.BaseTownOmDataEntity;
import com.hopes.bizz.poll.town.entity.PollTownMinEntity;
import com.hopes.bizz.poll.town.mapper.PollTownMinMapper;
import com.hopes.bizz.poll.town.service.PollTownMinService;
import com.hopes.bizz.poll.town.vo.PollTownAnalysisDataVO;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * ${tableComment}
 *
 * @author pig
 * @date 2023-09-26 18:57:10
 */
@Service("pollTownMin")
public class PollTownMinServiceImpl extends ServiceImpl<PollTownMinMapper, PollTownMinEntity> implements PollTownMinService {

    @Override
    public void batchSaveData(List<BaseTownOmDataEntity> list) {
        List<PollTownMinEntity> dataList = list.stream().map(data -> BeanUtil.copyProperties(data, PollTownMinEntity.class)).collect(Collectors.toList());
        saveBatch(dataList);
    }

    @Override
    public Page<BaseTownOmDataEntity> queryTownHisData(Page page, TownHisDataDTO townHisDataDTO) {
        return   baseMapper.selectPage(page, Wrappers.lambdaQuery(PollTownMinEntity.class)
                .eq(ObjectUtil.isNotNull(townHisDataDTO.getTownId()), PollTownMinEntity::getTownId, townHisDataDTO.getTownId())
                .ge(PollTownMinEntity::getDataTime, townHisDataDTO.getBeginTime())
                .le(PollTownMinEntity::getDataTime, townHisDataDTO.getEndTime()));
    }

    @Override
    public PollTownAnalysisDataVO trendAnalysis(TownHisTrendDTO townHisTrendDTO) {
        ArrayList<String> timeList = new ArrayList<>();
        List<PollTownMinEntity> hisData = baseMapper.selectList(Wrappers.lambdaQuery(PollTownMinEntity.class)
                .eq(PollTownMinEntity::getTownId, townHisTrendDTO.getTownId())
                .le(PollTownMinEntity::getDataTime, Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant()))
                .ge(PollTownMinEntity::getDataTime, Date.from(LocalDateTime.now().minusDays(1).atZone(ZoneId.systemDefault()).toInstant())));
        hisData.forEach(item -> timeList.add(item.getDataTimeStr()));

        List<Double> data = buildData(hisData,townHisTrendDTO.getFactorCode());
        PollTownAnalysisDataVO pollTownAnalysisDataVO = new PollTownAnalysisDataVO();
        pollTownAnalysisDataVO.setDataList(data);
        pollTownAnalysisDataVO.setTimeList(timeList);
        return pollTownAnalysisDataVO;
    }

    @Override
    public void alter(PollTownDataAlterDto pollTownDataAlterDto) {
        PollTownMinEntity entity = new PollTownMinEntity();
        entity.setDataJson(pollTownDataAlterDto.getDataJson());
        baseMapper.update(entity,Wrappers.lambdaUpdate(PollTownMinEntity.class)
                .eq(PollTownMinEntity::getId,pollTownDataAlterDto.getId()));
    }

}
